//全选class  : check-all check
//单选class  : check-one check

var check = getClass("check");
var cartTable = document.getElementById("cartTable");
var tr = cartTable.tBodies[0].rows;
// 选中件数
var selectedTotal = document.getElementById("selectedTotal");
// 总价格
var priceTotal = document.getElementById("priceTotal");
var foot = document.getElementById("foot");
var selectedViewList = document.getElementById("selectedViewList");
//循环check 选中的
for (var i = 0; i < check.length; i++) {
    check[i].onclick = function () {
        if (this.className == "check-all check") {
            for (var i = 0; i < check.length; i++) {
                check[i].checked = this.checked
            }
        }
        getTotal();
    }
}
// 点击foot出现已选商品
foot.onclick = function () {
    if (selectedTotal.innerHTML != 0) {
        this.className = "foot show"
    } else {
        this.className = "foot"
    }

}
//点击底部列表，取消选择
selectedViewList.onclick = function (e) {
    var e = window.event || e;
    var tar = e.target || e.serElement;
    if (tar.nodeName == "SPAN") {
        //就把对应的商品前的，复选框的状态为fasle
        tr[tar.getAttribute("index")].getElementsByTagName("input")[0].checked = false;
        //    tr[tar.getAttribute("index")].getElementsByTagName("input")[0].checked = false;
        tr[tar.getAttribute("index")].getElementsByTagName("input")[0].onclick();
    }
}
//点击一行中的数量加减，删除
for (var i = 0; i < tr.length; i++) {
    tr[i].onclick = function (e) {
        var e = window.event || e;
        var tar = e.target || e.serElement;
        //获取input 数量标签
        var inputs = this.getElementsByTagName("input")[1];
        //获取值
        var val = parseInt(inputs.value);
        switch (tar.className) {
            case "add": 
                 inputs.value=val+1;
                 subTotal(this);
                break;
            case "reduce":
                if(val<=1){
                    inputs.value=1;   
                }else{
                    inputs.value = val-1;
                }
                subTotal(this);
                break;
            case "delete":
                // alert("sahnchu");
                var con=confirm("确认删除吗");
                if(con){
                  this.parentNode.removeChild(this);
                }
                break;
        }
        getTotal();

    }
}
function subTotal(tr){

    var td2 = tr.cells[2];
    var td4 = tr.cells[4];
    var inputs = tr.getElementsByTagName("input")[1];
    td4.innerHTML= (td2.innerHTML * inputs.value).toFixed(2);
    //单价*数量=小计

    //实现小计功能，实现-显示或者隐藏功能
    var reduce = tr.getElementsByTagName("span")[1];
    if(inputs.value==1){
        reduce.innerHTML = "";
    }else{
        reduce.innerHTML = "-";
    }


}
function getTotal() {
    var select = 0;
    var price = 0;
    //展示底部已选商品
    var list = "";
    for (var i = 0; i < tr.length; i++) {
        //如果是复选框选中的状态，才能把值放入进去
        if (tr[i].getElementsByTagName("input")[0].checked) {
            select += parseInt(tr[i].getElementsByTagName("input")[1].value);
            price += parseFloat(tr[i].cells[4].innerHTML);
            list += "<div><img src='" + tr[i].getElementsByTagName("img")[0].src + "'><span index=" + i + ">取消选择</span></div>"
        }
    }
    selectedTotal.innerHTML = select;
    priceTotal.innerHTML = price.toFixed(2);
    selectedViewList.innerHTML = list;

}
//函数==》实现获取class名称的
function getClass(check) {
    var arr = [];
    //获取所有的节点
    var doms = document.getElementsByTagName("*");

    for (var i = 0; i < doms.length; i++) {
        if (doms[i].className == check || doms[i].className.indexOf(" " + check) != -1 || doms[i].className.indexOf(check + " ") != -1) {
            arr.push(doms[i])
        }
    }

    //    返回class为 check 的数组
    return arr;

}
// alert(getClass("check").length)